package the.broccoli.setting.service.impl;

import com.github.pagehelper.PageHelper;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.wisteria.framework.core.exception.BusinessException;
import org.wisteria.framework.web.QueryResult;
import org.wisteria.framework.web.mvc.vo.ConditionVO;
import the.broccoli.setting.entity.Dict;
import the.broccoli.setting.mapper.DictMapper;
import the.broccoli.setting.service.IDictService;
import org.wisteria.framework.web.mvc.service.AbstractSuperService;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 字典项 服务实现类
 * </p>
 *
 * @author lei.liu
 * @since 2019-11-27
 */
@Service
public class DictServiceImpl extends AbstractSuperService<DictMapper, Dict> implements IDictService {

    @Transactional
    @Override
    public boolean save(Dict entity) {
        checkDict(entity);
        return super.save(entity);
    }

    @Transactional
    @Override
    public boolean updateById(Dict entity) {
        checkDict(entity);
        return super.updateById(entity);
    }

    private void checkDict(Dict entity) {
        String dictCode = entity.getDictCode();
        if (StringUtils.hasText(dictCode) && this.baseMapper.selectByCode(dictCode) != null) {
            throw new BusinessException("字典项编码已经存在相同的");
        }
    }
}
